from flask import Blueprint,jsonify,request,abort
from flask_restful import Api,Resource
from app.models import *
procurement0= Api()

class Procurement1(Resource):
    def get(self):
        an = Procurement.query.all()
        return jsonify({"an": list(map(con4, an))})

    def post(self, uid):
        ap = Procurement(
            request.json["name"],
            request.json["number"],
            request.json["time"],
            request.json["company"],
            request.json["contact_person"],
            request.json["telephone_number"],
            request.json["amount_payable"],
            request.json["paid_amount"]
        )
        db.session.add(ap)
        db.session.commit()
        return jsonify({"ap": con4(ap)})
procurement0.add_resource(Procurement1, "/Procurement1/")


class Procurement2(Resource):
    def get(self, uid):
        ao = Procurement.query.filter_by(id=uid).first()
        if ao is None:
            abort(404)
        return jsonify({"ao": con4(ao)})

    def put(self, uid):
        aq = Procurement.query.get(uid)
        aq.name = request.json.get("name")
        aq.number = request.json.get("number")
        aq.time = request.json.get("time")
        aq.company = request.json.get("company")
        aq.contact_person = request.json.get("contact_person")
        aq.telephone_number = request.json.get("telephone_number")
        aq.amount_payable = request.json.get("amount_payable")
        aq.paid_amount = request.json.get("paid_amount")
        return jsonify({"aq": con4(aq)})


procurement0.add_resource(Procurement2, "/Procurement2/<int:uid>/")